using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace RUPS
{
    public partial class AdaugaComandaClientForm : Form
    {
        DateTime dataCom, dataLiv;
        string idCl;

        public AdaugaComandaClientForm()
        {
            InitializeComponent();
            CenterToScreen();
        }

        private void AdaugaComandaClientForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'masivDBDataSet10.clienti' table. You can move, or remove it, as needed.
            //this.clientiTableAdapter.Fill(this.masivDBDataSet10.clienti);
            SqlDataAdapter da = new SqlDataAdapter("select * from clienti", clientiTableAdapter.Connection);
            DataSet ds = new DataSet();
            da.Fill(ds, "clienti");
            comboBox1.DataSource = null;
            comboBox1.Items.Clear();
            foreach (DataRow r in ds.Tables["clienti"].Rows)
            {
                string item = r["id"].ToString() + "," + r["nume"].ToString() + r["prenume"].ToString();
                comboBox1.Items.Add(item);
            }

        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            dataCom = dateTimePicker1.Value;
            dateTimePicker2.Value = dateTimePicker1.Value.AddDays(8);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string detalii = comboBox1.SelectedItem.ToString();
            string[] s = detalii.Split(',');
            string idClient = s[0];
            idCl = idClient;
            SqlDataAdapter da = new SqlDataAdapter("select * from clienti where id=" + idClient, clientiTableAdapter.Connection);
            DataSet ds = new DataSet();
            da.Fill(ds, "clienti");
            textBox1.Text = "";
            foreach (DataRow r in ds.Tables["clienti"].Rows)
            {
                if (r["adresa1"].ToString() != null)
                    textBox1.Text += "Adresa: " + r["adresa1"].ToString() + "\r\n";
                if (r["adresa2"].ToString() != null)
                    textBox1.Text += "Adresa: " + r["adresa2"].ToString() + "\r\n";
                if (r["localitate"].ToString() != null)
                    textBox1.Text += "Localitate: " + r["localitate"].ToString() + "\r\n";
                if (r["telefon"].ToString() != null)
                    textBox1.Text += "Tel:" + r["telefon"].ToString() + "\r\n";
                if (r["email"].ToString() != null)
                    textBox1.Text += "Email:" + r["email"].ToString() + "\r\n";
                if (r["tip"].ToString() != null)
                    textBox1.Text += "Tip:" + r["tip"].ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlCommand comm = this.clientiTableAdapter.Connection.CreateCommand();
            comm.CommandText = "insert into comenziClienti(idClient,dataComanda,dataLivrare) values(" +
                idCl + ",'" + dataCom.ToString() + "','" + dataLiv.ToString() + "')";
            if (clientiTableAdapter.Connection.State == ConnectionState.Closed)
                clientiTableAdapter.Connection.Open();
            comm.ExecuteNonQuery();
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            dataLiv = dateTimePicker2.Value;
        }
    }
}